require 'watir'
require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'


class TC_ManageSpamRules_test < Test::Unit::TestCase
   
    def startup
       $RegularExpressions = '"[\^$.|?*+()"+\n "\b[A-Z0-9._%+-]@[A-Z0-9.-]+\.[A-Z]{2,4}\b"'
       $Forbidden = 'forbidden\n words\n added'
       $EmailRegExp = '^([\w,.])+\@([\w,.]+)$'
       $IPFrequency ='000.000.000.000;127.0.0.1'
       $SpamTerms ='-online;4u;adipex;advicer;ambien;art works;baccarrat;blackjack;bllogspot;booker;byob;car-rental-;carisoprodol;cartwish;casino;chatroom;china;cialis;coolhu;credit-;cwas;cyclen;cyclobenzaprine;dating-;day-trading;debt-consolidation;download;drug;discreetordering;duty-free;dutyfree;edit1;equityloans;fioricet;flowers-;free;freenet;gambling;health-insurancedeals-;holdem;homeequityloans;homefinance;hotel-deals;hotele-site;hotelse-site;incest;insurance-quotesdeals-;insurancedeals-;jrcreations;levitra;loan;macinstruct;mortgage-4;mortgagequotes;online-gambling;onlinegambling-;ottawavalleyag;ownsthis;paxil;pen1s;penis;pharmacy;phentermine;poker;porn;poze;rental-car-;right-top;ripoffs;roulette;search-browse;sex;shemale;shoes;slot;soma;sysworkds;taboo;teen;texas;thorcarlson;top-site;top-e-;tramadol;trim-spa;ultram;valeofglamorganconservatives;viagra;vioxx;xanax;zolus'
       @@ie = Watir::IE.start($SITE_URL)
      end
    
    
    def ie
       if defined? @@ie
         @@ie
       else
         startup
        end
      end
    
    def test_01_verifyHomeText
       assert(ie.contains_text(/Community Server/))		
      end
      
    def test_02_verifyLoginLinksExist
       if !(ie.link(:text, 'Sign in').exists?)
         ie.goto($SITE_URL + 'logout.aspx')
         ie.refresh
         ie.goto($SITE_URL)
         ie.refresh
        end
	    end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
      end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	    end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
      
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
      end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
      end
    
    def test_08_navigateToSiteAdministration
        ie.goto($SITE_URL + 'controlpanel/Settings/default.aspx')
        assert(ie.contains_text('Site Administration'))      
      end
    
    def test_09_verifyLinksExist
        # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Setup').exists?)
        assert(ie.link(:text, 'Manage Licenses').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
      end
      
    def test_10_navigateToManageSpamRules
        ie.goto($SITE_URL + 'controlpanel/tools/ManageSpamRules.aspx')
        assert(ie.text_field(:id,/AutoModerateValue$/).exists?)
        assert(ie.text_field(:id,/AutoDeleteValue$/).exists?)
        ie.text_field(:id,/AutoModerateValue$/).value ='7'
        ie.text_field(:id,/AutoDeleteValue$/).value = '12'
        table1=ie.table(:index,9)
        # Total URLs  3,0
        table1[2][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[2][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).value = '3'
        assert(ie.frame(:index,1).text_field(:id,/ctl06_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl06_Text$/).value = '2'
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
         
        # Comment Length Blog Spam Rule  5,10
        sleep 2
        table1[3][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[3][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).value = '7'
        assert(ie.frame(:index,1).text_field(:id,/ctl06_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl06_Text$/).value = '12'
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
         
        
        # Flood from IP Blog Spam Rule  1,5,60
        sleep 2
        table1[4][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[4][3].link(:text,'Configure').click
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).value = '2'
        assert(ie.frame(:index,1).text_field(:id,/ctl06_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl06_Text$/).value = '12'
        assert(ie.frame(:index,1).text_field(:id,/ctl07_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl07_Text$/).value = '62'
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
         
        
        # Regex Blog Spam Rule   5
        sleep 2
        table1[5][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[5][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).set('7')
        sleep 5
        assert(ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl06$ctl03').exists?)
        sleep 2
        ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl06$ctl03').set($RegularExpressions)
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
        
        
        # Email Blog Spam Rule    5
        sleep 2
        table1[6][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[6][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).set('7')
        sleep 5
        assert(ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl06$ctl03').exists?)
        sleep 2
        ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl06$ctl03').set($EmailRegExp)
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
        
        # IP Frequency    1,5,60,000.000.000.000;127.0.0.1
        sleep 2
        table1[7][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[7][3].link(:text,'Configure').click
        sleep 5 
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).value = '2'
        assert(ie.frame(:index,1).text_field(:id,/ctl06_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl06_Text$/).value = '12'
        assert(ie.frame(:index,1).text_field(:id,/ctl07_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl07_Text$/).value = '62'
        sleep 5
        assert(ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl08$ctl03').exists?)
        sleep 2
        ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl08$ctl03').set($IPFrequency)
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
        
        # Possible Spam Terms  1,2,-online;4u;adipex;advicer;ambien;art works;baccarrat;blackjack;bllogspot;booker;byob;car-rental-;carisoprodol;cartwish;casino;chatroom;china;cialis;coolhu;credit-;cwas;cyclen;cyclobenzaprine;dating-;day-trading;debt-consolidation;download;drug;discreetordering;duty-free;dutyfree;edit1;equityloans;fioricet;flowers-;free;freenet;gambling;health-insurancedeals-;holdem;homeequityloans;homefinance;hotel-deals;hotele-site;hotelse-site;incest;insurance-quotesdeals-;insurancedeals-;jrcreations;levitra;loan;macinstruct;mortgage-4;mortgagequotes;online-gambling;onlinegambling-;ottawavalleyag;ownsthis;paxil;pen1s;penis;pharmacy;phentermine;poker;porn;poze;rental-car-;right-top;ripoffs;roulette;search-browse;sex;shemale;shoes;slot;soma;sysworkds;taboo;teen;texas;thorcarlson;top-site;top-e-;tramadol;trim-spa;ultram;valeofglamorganconservatives;viagra;vioxx;xanax;zolus
        sleep 2
        table1[8][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[8][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).set('7')
        assert(ie.frame(:index,1).text_field(:id,/ctl06_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl06_Text$/).set('7')
        sleep 5
        assert(ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl07$ctl03').exists?)
        sleep 2
        ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl07$ctl03').set($SpamTerms)
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
        
        # Anonymous User Blog Spam Rule   1 , -5 , -25 
        sleep 2
        table1[9][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[9][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).value = '2'
        assert(ie.frame(:index,1).text_field(:id,/ctl06_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl06_Text$/).value = '12'
        assert(ie.frame(:index,1).text_field(:id,/ctl07_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl07_Text$/).value = '62'
        sleep 3
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
        
        # Recent Duplicate Blog Spam Rule    5,20
        sleep 2
        table1[10][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[10][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).value = '7'
        sleep 2
        assert(ie.frame(:index,1).text_field(:id,/ctl06_Text$/).exists?)
        ie.frame(:index,1).text_field(:id,/ctl06_Text$/).value = '32'
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
        
        # Known Spam Terms  5  
        sleep 2
        table1[11][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[11][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).set('7')
        sleep 5
        assert(ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl06$ctl03').exists?)
        sleep 2
        ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl06$ctl03').set($Forbidden)
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
        
        # Forbidden Word Blog Spam 5    
        sleep 2
        table1[12][1].checkbox(:id,/RuleEnabled$/).set
        sleep 2
        table1[12][3].link(:text,'Configure').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/ctl05_Text$/).exists?)
        sleep 2
        ie.frame(:index,1).text_field(:id,/ctl05_Text$/).set('7')
        sleep 5
        assert(ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl06$ctl03').exists?)
        sleep 2
        ie.frame(:index,1).text_field(:name,'ctl00$bcr$ConfigurableAddonForm$ctl00$ConfigurationForm$ctl06$ctl03').set($Forbidden)
        sleep 2
        assert(ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).exists?)
        ie.frame(:index,1).button(:id,/RestoreDefaultsButton$/).click
        sleep 2
        assert(ie.frame(:index,1).button(:id,/Save$/).exists?)
        ie.frame(:index,1).button(:id,/Save$/).click
        table1[2][1].checkbox(:id,/RuleEnabled$/).clear
        table1[3][1].checkbox(:id,/RuleEnabled$/).clear
        table1[4][1].checkbox(:id,/RuleEnabled$/).clear
        table1[5][1].checkbox(:id,/RuleEnabled$/).clear
        table1[6][1].checkbox(:id,/RuleEnabled$/).clear
        table1[7][1].checkbox(:id,/RuleEnabled$/).clear
        table1[8][1].checkbox(:id,/RuleEnabled$/).clear
        table1[9][1].checkbox(:id,/RuleEnabled$/).clear
        table1[10][1].checkbox(:id,/RuleEnabled$/).clear
        table1[11][1].checkbox(:id,/RuleEnabled$/).clear
        table1[12][1].checkbox(:id,/RuleEnabled$/).clear
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
        assert(ie.button(:id,/RefreshButton$/).exists?)
        ie.button(:id,/RefreshButton$/).click
      end
      
    def test_99_closeIE
        ie.close
      end      
        
end